<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
class ZzJump {
	/**
	* Jump to an other label
	* @param string|int $labels Path label or the lid
	* @param bool $altPuZZle True if you want the alternatif puzzle
	* @return string
	*/
	static function to($labels,$altPuZZle = false) {
		if($altPuZZle)
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$labels.'&altPuZZle');
		else
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$labels);
	}

	/**
	* Jump to the other puzzle.
	* @param int $id Label id (optional)
	* @return string
	*/
	function toTheOtherPuZZle($id = null) {
		//If it's a new label, the GET_Label look like '{somehing}-2-1'.
		if(!is_null($id))
			if(preg_match('!^.*-!s',$_GET['label']))
				$getLabel = $id.'-'.preg_replace('!^.*-!s','',$_GET['label']);
			else
				$getLabel = $id;
		else
			$getLabel = $_GET['label'];
		if(isset($_GET['altPuZZle']))
			if($_GET['altPuZZle'] == 'false')
				header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$getLabel.'&altPuZZle');
		header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$getLabel);
	}

	/**
	* "Saute" vers un label enfant
	* @param string $arg l'identifiant du label enfant
	* @param bool $altPuZZle Si on veux le puzzle alternatif
	* @return void
	*/
	static function toChild($arg,$altPuZZle = false) {
		$getLabel = preg_replace('!{.*}-!s','',$_GET['label']);
		if($altPuZZle)
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$arg.'-'.$getLabel.'&altPuZZle');
		else
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$arg.'-'.$getLabel);
	}

	/**
	* "Saute" au label parent
	* @param bool $altPuZZle Si on veux le puzzle alternatif
	* @return string
	*/
	static function toParent($altPuZZle = false) {
			$_labels = explode('-',$_GET['label']);
			$url = '';
			for($i = 1; $i<count($_labels)-1;$i++)
				$url .= $_labels[$i].'-';
			$url .= $_labels[count($_labels)-1];
			if($altPuZZle)
				$url .= '&altPuZZle';
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?label='.$url);
	}
}
?>
